using System;
using System.IO;
using NUnit.Framework;
using Syno.Core.Output;
using Syno.Core.Output.DataDisplay;
using Syno.Core.Output.DataLoader;
using Syno.Core.Output.OutputFormat;
using Syno.Core.Xml;

namespace Syno.Tests
{
    [TestFixture]
    public class Integration
    {

        [Test,Category("Integration"),Explicit]
        public void IntegrationTest_CreatAnHtmlFileOutputFromLog_ShouldInterperateAllRequist()
        {
            string outputFile = @"output.html";
            IOutput output = new HtmlFileOutput(outputFile)
                                 {
                                     Template = "<html><header><title>Report</title></header><body><h1>Report Summary</h1><div>"+GetAll()+"</div></body></html>"
                                 };
            Exec(output);
            Assert.That(File.Exists(outputFile));
        }


        [Test, Category("Integration")]
        public void IntegrationTest_LoadsTheImaginationSettingGolfSettingsFile_HtmlGenerated()
        {
       
            var fileName = @"C:\Dev\Projects\Ian\Advantage.Golf\tools\Syno\ig.xml";
            var xmlReader = new Core.Xml.XmlReader();
            
            var synoConfig = xmlReader.ReadFile(fileName);
            var synoConfigProcessor = new SynoConfigProcessor();
            var dateTime = DateTime.Now;

            synoConfigProcessor.Run(synoConfig, "default");
            
            Console.Out.WriteLine((string.Format("Done [{0}]", DateTime.Now - dateTime)));
            
        }

        [Test, Category("Integration")]
        public void IntegrationTest_LoadsTheImaginationSettingGolfSettingsFile_EmailRecieved()
        {

            var fileName = @"C:\Dev\Projects\Ian\Advantage.Golf\tools\Syno\ig.xml";
            var xmlReader = new Core.Xml.XmlReader();

            var synoConfig = xmlReader.ReadFile(fileName);
            var synoConfigProcessor = new SynoConfigProcessor();
            var dateTime = DateTime.Now;

            synoConfigProcessor.Run(synoConfig, "localEmail");

            Console.Out.WriteLine((string.Format("Done [{0}]", DateTime.Now - dateTime)));

        }
       
        [Test, Category("Integration"), Explicit]
        public void IntegrationTest_EmailOutputFromLog_ShouldInterperateAllRequist()
        {
            IOutput output = new EmailFormatOutput(Helper.Smtp(),"rolf.wessels@gmail.com","rolf.wessels@gmail.com")
            {
                Body = "<html><header><title>Report</title></header><body><h1>Report Summary</h1><div>" + GetAll() + "</div></body></html>",
                Subject = "Some report"
            };
            Exec(output);
        }

        private string GetAll()
        {
            return "$ErrorCounts $Top5ClubRounds $TopClubPlayerGraph $TopClubPlaysGg <pre>$TopClubPlaysText</pre> ";
        }


        private void Exec(IOutput output)
        {
            IDatabase connection =
                new SqlServer("Data Source=Zino;Initial Catalog=AdvantageGolfTest;;User ID=rolf;Password=Password123;", 300);
            IDataLoader dataLoader = new SqlDataLoader(connection)
            {
                Sql = "SELECT [Level] , COUNT(*) Count FROM [AVG_Log] GROUP BY [Level]",
            };
            IDataDisplay dataDisplay = new HtmlTableDisplay()
            {
                Heading = "Log level count"
            };


            var outputManager = new OutputManager();
            outputManager.AddOutput(output);
            outputManager.AddDataHolder(new DataHolder("ErrorCounts", dataLoader, dataDisplay));
            outputManager.AddDataHolder(
                new DataHolder("Top5ClubRounds",
                               new SqlDataLoader(connection)
                                   {
                                       Sql = "select top 5 ClubTitle as 'Title' , count(*) Rounds from AVG_Transaction where TransactionDate > DATEADD(MONTH,-3,GETDATE()) and TransactionType = 'PlayerRound' group by ClubTitle order by COUNT(*) desc ",
                                   }, new HtmlTableDisplay()
                                          {
                                              Heading = "Top 5 club rounds in 3 months"
                                          }));
            outputManager.AddDataHolder(
                new DataHolder("TopClubPlaysGg",
                               new SqlDataLoader(connection)
                                   {
                                       Sql = @"select CONVERT(VARCHAR(8), TransactionDate, 3) AS 'Transaction Date' ,COUNT(*) cnt
                            from AVG_Transaction
                            where TransactionType = 'PlayerRound'
                            and ClubID in (select top 1 ClubID  from AVG_Transaction where TransactionDate > DATEADD(MONTH,-3,GETDATE())and TransactionType = 'PlayerRound' group by ClubID order by COUNT(*) desc )
                            and TransactionDate > DATEADD(MONTH,-3,GETDATE())
                            GROUP BY CONVERT(VARCHAR(8), TransactionDate, 3),ClubTitle",
                                   }, new GoogleSingleLineGraphDisplay(500, 100, "cnt", "Transaction Date", null)
                                          {
                                              Heading = "Top club players per day"
                                          }));
            outputManager.AddDataHolder(
               new DataHolder("TopClubPlaysText",
                              new SqlDataLoader(connection)
                                  {
                                      Sql = @"select CONVERT(VARCHAR(8), TransactionDate, 3) AS 'Transaction Date' , COUNT(*) cnt
                            from AVG_Transaction
                            where TransactionType = 'PlayerRound'
                            and ClubID in (select top 1 ClubID  from AVG_Transaction where TransactionDate > DATEADD(MONTH,-3,GETDATE())and TransactionType = 'PlayerRound' group by ClubID order by COUNT(*) desc )
                            and TransactionDate > DATEADD(MONTH,-3,GETDATE())
                            GROUP BY CONVERT(VARCHAR(8), TransactionDate, 3),ClubTitle",
                                  }, new TextTableDisplay()
                                         {
                                             Heading = "Top club per day"
                                         }));
            SqlDataLoader sqlDataLoader = new SqlDataLoader(connection)
                                              {
                                                  Sql = @"select CONVERT(VARCHAR(8), dates.thedate , 3) AS 'Transaction Date' , 
	                               PlayerFullName,
	                               (select COUNT(*) from AVG_Transaction tt where tt.TransactionType = 'PlayerRound' and CONVERT(VARCHAR(8), tt.TransactionDate, 3) = CONVERT(VARCHAR(8), dates.thedate , 3) and tt.PlayerFullName = names.PlayerFullName  ) cnt
                            from dbo.ExplodeDays(DATEADD(MONTH,-3,GETDATE()),GETDATE()) dates,
                            (select top 3 PlayerFullName  from AVG_Transaction where TransactionDate > DATEADD(MONTH,-3,GETDATE())and TransactionType = 'PlayerRound' group by PlayerFullName order by COUNT(*) desc) as names
",
                                              };
            outputManager.AddDataHolder(
               new DataHolder("TopClubPlayerGraph",
                              sqlDataLoader, new GoogleSingleLineGraphDisplay(500, 100, "cnt", "Transaction Date", "PlayerFullName")
                                                 {
                                                     Heading = "Top club players per day"
                                                 }));
            outputManager.AddDataHolder(
               new DataHolder("TopClubPlayerTable",
                              sqlDataLoader, new HtmlTableDisplay()
                                                 {
                                                     Heading = "Top club players per day"
                                                 }));
            outputManager.ProcessAll();

            
            
        }

    }

    
}